package amit.calc.form;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;

import amit.calc.form.table.CalcTableModel;
import amit.calc.form.table.NumRenderer;

/**
 * 
 * @author ido
 *
 */
public class MainForm extends javax.swing.JPanel implements PropertyChangeListener {

	
	private RentalPanel rentalPanel;
	private JButton updateTableButton;
	private CalcTableModel tableModel;
	private JTable table;
	
	private JPanel tablePanel;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new MainForm());
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public MainForm() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setLayout(new BorderLayout(12,12));
			updateTableButton = new JButton("Go");
			updateTableButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buildTable();
				}
			});
			updateTableButton.setMnemonic('G');
			rentalPanel = new RentalPanel();
			rentalPanel.getRentField().addPropertyChangeListener("value", this);
			add(rentalPanel, BorderLayout.WEST);
			
			JPanel bottomPanel = new JPanel();
			bottomPanel.add(updateTableButton);
			add(bottomPanel,BorderLayout.SOUTH);
			
			tablePanel = new JPanel(new BorderLayout());
//			tablePanel.setPreferredSize(new Dimension(500,300));
			tablePanel.setBorder(BorderFactory.createTitledBorder("Line of Credit - Rapid Reduction"));
			add(tablePanel, BorderLayout.CENTER);
			//setPreferredSize(new Dimension(800,500));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Yearly Rent	5427.692308
Loan Value	39900
Interest Rate 	5.25%
Yearly Interest	2094.75
Yearly Maintenance	1200
Yearly Insurance	200
Yearly Mgmt Fee	250
Yearly CF	1682.942308
principal = Monthly CF	140.2451923

	 *
	 */
	public double getFirstPrincipal() {
		double yearlyRent = rentalPanel.getRent() * 12.0d * 
		 	Double.parseDouble(rentalPanel.getOccupancyRateSpinner().getValue().toString());
		double loan = rentalPanel.getLoan();
		double interest = rentalPanel.getInterest();
		double yearlyInterest  = loan * interest;
		double yearlyCF = yearlyRent - 
			yearlyInterest - 
			Integer.parseInt(rentalPanel.getMaintFeeField().getValue().toString()) -
			Integer.parseInt(rentalPanel.getManagFeeField().getValue().toString()) - 
			Integer.parseInt(rentalPanel.getHomeOwnerFeeField().getValue().toString());
		double monthCF = yearlyCF /12.0;
		return monthCF;
	}
	
	public void buildTable() {
		//loan, int	years, double interest, double rent
		double monthCF = getFirstPrincipal();
		tableModel = new CalcTableModel(rentalPanel.getLoan(), rentalPanel.getYears(),
				rentalPanel.getInterest(), rentalPanel.getRent(), monthCF);
		table = new JTable(tableModel);
		table.getColumnModel().getColumn(0).setMaxWidth(40);
		table.setDefaultRenderer(Double.class, new NumRenderer());
		table.setCellSelectionEnabled(true);
		
		JScrollPane sp = new JScrollPane(table);
		tablePanel.removeAll();
		tablePanel.add(sp, BorderLayout.CENTER);
		
		TotalsPanel tp = new TotalsPanel(tableModel.getTotalRowNo(), tableModel.getTotalPrincipal(),tableModel.getTotalInterest(),
				tableModel.getTotalTotal());
		tablePanel.add(tp, BorderLayout.SOUTH);
		
		tablePanel.revalidate();
		revalidate();
	}

	/**
	 * 
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		//System.out.println("MainForm: prop change:"+evt );
		// for now only the rent is driving the table
		buildTable();
	}

}
